<?php

class ResponsibleController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }
    
    public function indexAction()
    {         
        $view = $this->_getView('Verantwoordelijken');  
        $responsibles = $this->_getAllResponsibles();  
        $view->responsibles = $responsibles;
    }
    
    public function editAction()
    {   
        $view = $this->_getView('Aanpassen verantwoordelijken');
        $form = $this->_getEditResponsibleForm();
        
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $view = $this->_getView('Fout - Toevoegen');  
                
                // Responsible toevoegen 
                $values = $form->getValues();

                $responsible_values['id'] = $values['responsible_id'];
                $responsible_values['givenname'] = $values['responsible_givenname'];
                $responsible_values['familyname'] = $values['responsible_familyname'];
                $responsible_values['fotoUrl'] = $values['responsible_fotoUrl'];
       
                $responsible = new Application_Model_Responsible($responsible_values);
                
                $responsibleMapper = new Application_Model_ResponsibleMapper();
                $responsibleMapper->save($responsible);

                return $this->_helper->redirector('index', 'responsible');                
            }
        }else{
            $id = (string)$this->getRequest()->getParam('id');
            
            $responsible = $this->_getViewResponsible($id);
            //$promotions->
            if(isset($responsible))
            {
                $form->populate($responsible->toArray());         
            }            
            $view->form = $form; 
        }   
    }

    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           // Zend_View_Helper_HeadTitle
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); // Zend_View_Helper_HeadLink

        return $view;
    }
    
    private function _getEditResponsibleForm()
    {   
        return new Application_Form_EditResponsible(array(
            'action' => $this->view->baseUrl() . '/responsible/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getAllResponsibles()
    {
        $responsibleMapper = new Application_Model_ResponsibleMapper();
        $responsibleArray  = $responsibleMapper->fetchAll();

        return $responsibleArray;
    }
    
    private function _getViewResponsible($id)
    {    
        $responsibleMapper = new Application_Model_ResponsibleMapper();
        $responsibleArray  = $responsibleMapper->fetchById($id);

        return $responsibleArray;
    }
}

